let prisma = require("../util/prismaBase")
let returnUtil = require("../util/returnUtil")


exports.allMachines = async (req, res) => {
    console.log("查询所有",req.body.params)
    try {
        let result = await prisma.tb_departMachine.findMany({
            where: req.body.params,
            include:{
                depart:true
            },
            orderBy: {
                create_time: 'desc'
            }
        })
        res.send(returnUtil.getReturn(result))
    } catch (error) {
        console.log(error);
        res.send(returnUtil.getError(JSON.stringify(error)))
    }

}

exports.listMachine = async (req, res) => {
    console.log("获取列表", req.body.params)
    if (typeof (req.body.pageIndex) == 'undefined') {
        req.body.pageIndex = 0
    }
    if (typeof (req.body.pageSize) == 'undefined') {
        req.body.pageSize = 10
    }
    try {
        let result = await prisma.tb_departMachine.findMany({
            where: req.body.params,
            skip: req.body.pageIndex,
            take: req.body.pageSize,
            include:{
                depart:true
            },
            orderBy: {
                create_time: 'desc'
            }
        })
        let count = await prisma.tb_departMachine.count({
            where: req.body.params,
        })
        res.send(returnUtil.getReturn(result, { count: count }))
    } catch (error) {
        console.log(error);
        res.send(returnUtil.getError(JSON.stringify(error)))
    }
}

exports.addMachine = async (req, res) => {
    let params = req.body.params;
    console.log("添加机器", params)
    try {
        let result = await prisma.tb_departMachine.create({
            data: params
        })
        res.send(returnUtil.getReturn(result))
        
    } catch (error) {
        console.log("添加机器异常error:",error)
        res.send(returnUtil.getError("添加机器异常"))
    }
   
}

exports.delMachine = async (req, res) => {
    let params = req.body.id;
    console.log("删除机器", params)
    try {
        let result = await prisma.tb_departMachine.delete({
            where:{
                id:req.body.id
            }
        })
        res.send(returnUtil.getReturn(result))
        
    } catch (error) {
        console.log("删除机器异常error:",error)
        res.send(returnUtil.getError("删除机器异常"))
    }
   
}

exports.editMachine = async (req, res) => {
    let params = req.body.params;
    let id = req.body.id
    console.log("编辑机器", params,"id:",id)
    try {
        let result = await prisma.tb_departMachine.update({
            data: params,
            where:{
                id:id
            }
        })
        res.send(returnUtil.getReturn(result))
        
    } catch (error) {
        console.log("编辑机器异常error:",error)
        res.send(returnUtil.getError("编辑机器异常"))
    }
   
}